<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/3-15:14
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\service\CategoryService;
    use Jrk\Trees;

    class Category extends AdminBase
    {


        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model=app()->model("Categorys");

            $this->service=new CategoryService();

            $this->del="文章栏目";

            $this->assign("tree", Trees::toFormatTree(CategoryService::selcectAll(), 'name'));


        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addCategory
         * @describe:添加栏目
         */
        public function addCategory(){
            if (IS_POST){
                $data = $this->request->post();

                $validate=new \app\admin\validate\Category();

                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                if($data['model_id'] == 4){
                    if(empty($params['url'])){
                       $this->error("外部链接不能为空");
                    }
                }

                if (isset($data['id'])){

                    addLog("栏目修改操作","修改栏目：".$data['name']);

                    $cat=$this->model->addAndEdit($data);
                }else{
                    addLog("栏目添加操作","新增菜单：".$data['name']);

                    $cat=$this->model->add($data);
                }
                return $cat;

            }

            $pid = $this->request->param("id/d");//父id

            $this->assign('pid',$pid);

            $model_select = model('models')->adminGetModelSelect();

            $this->assign('model_select',$model_select);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: editCategory
         * @describe:编辑
         */
        public function editCategory(){

            $model_select = model('models')->adminGetModelSelect();

            $this->assign('model_select',$model_select);

            $id   = $this->request->param("id/d");//父id

            $info = $this->service->getOne(['id'=>$id]);

            if (empty($info)){
                $this->assign('msg','当前id:'.$id.'错误');
                $this->fetch("public/404");
            }

            $this->assign("info",$info);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/5
         * @name: moveSelect
         * @describe:
         */
        public function moveSelect(){
            if(IS_POST){
                $param=$this->request->param();

                //移动的id
                $this->assign('ids',implode(',',$param['ids']));
                return $this->fetch();
            }
        }


    }